package cn.starlightsoftware.sherly.controller.admin.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.starlightsoftware.sherly.controller.admin.user.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.starlightsoftware.sherly.convertor.UserConvertor;
import cn.starlightsoftware.sherly.model.PageResult;
import cn.starlightsoftware.sherly.model.Result;
import cn.starlightsoftware.sherly.db.model.permission.UserRoleDO;
import cn.starlightsoftware.sherly.db.model.user.UserDO;
import cn.starlightsoftware.sherly.service.user.UserService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.starlightsoftware.sherly.model.Result.success;

/**
 * @author 谷子毅
 * @date 2024/4/25
 */
@Validated
@Tag(name = "用户")
@RestController
@RequestMapping("/system/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/page")
    @PreAuthorize("@ss.hasAnyScopes('system:user:page')")
    @Operation(summary = "用户分页")
    public Result<PageResult<UserRespVO>> page(@ParameterObject UserPageReqVO reqVO) {
        PageResult<UserDO> pageResult = userService.page(reqVO);
        if (CollUtil.isEmpty(pageResult.getResult())) {
            return success(PageResult.buildEmpty());
        }
        return success(PageResult.build(BeanUtil.copyToList(pageResult.getResult(), UserRespVO.class), pageResult.getTotal()));
    }

    @PostMapping("/save")
    @PreAuthorize("@ss.hasAnyScopes('system:user:save')")
    @Operation(summary = "用户新增")
    public Result<?> save(@Valid @RequestBody UserSaveReqVO reqVO) {
        userService.save(reqVO);
        return success();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasAnyScopes('system:user:update')")
    @Operation(summary = "用户修改")
    public Result<?> update(@Valid @RequestBody UserSaveReqVO reqVO) {
        userService.update(reqVO);
        return success();
    }

    @GetMapping("/detail")
    @PreAuthorize("@ss.hasAnyScopes('system:user:detail')")
    @Operation(summary = "用户详情")
    public Result<UserRespVO> detail(@ParameterObject UserDetailReqVO reqVO) {
        UserDO user = userService.detail(reqVO.getUserId());
        return success(BeanUtil.copyProperties(user, UserRespVO.class));
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasAnyScopes('system:user:delete')")
    @Operation(summary = "用户删除")
    public Result<?> delete(@Valid @RequestBody UserDetailReqVO reqVO) {
        userService.delete(reqVO.getUserId());
        return success();
    }

    @GetMapping("/role-list")
    @PreAuthorize("@ss.hasAnyScopes('system:user:role-list')")
    @Operation(summary = "用户角色详情")
    public Result<UserRoleRespDTO> roleList(@ParameterObject UserDetailReqVO reqVO) {
        List<UserRoleDO> userRoleList = userService.userRoleList(reqVO.getUserId());
        return success(UserConvertor.buildUserRoleRespVO(userRoleList));
    }

    @PostMapping("/role-update")
    @PreAuthorize("@ss.hasAnyScopes('system:user:role-update')")
    @Operation(summary = "用户角色修改")
    public Result<?> roleUpdate(@Valid @RequestBody UserRoleUpdateReqVO reqVO) {
        userService.userRoleUpdate(reqVO);
        return success();
    }


}
